<?php
/**
 * Created by PhpStorm.
 * User: Victor
 * Date: 2018/9/14
 * Time: 9:19
 */

namespace app\manage\controller;


use think\Db;
use think\Request;
class Pinzhong extends BaseController
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods:POST,GET");
        header("Access-Control-Allow-Headers:x-requested-with,content-type");
        header("Content-type:text/json;charset=utf-8");
    }
    public function index()
    {
        $data = input();
        if(empty($data['token'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $shuju = Db::query('SELECT *, (SELECT name FROM huobi WHERE heyue_zu.hb_id = huobi.id) AS bizhong, (SELECT name FROM huobi WHERE heyue_zu.yuan_hb_id = huobi.id) AS yuanbizhong, (SELECT name FROM huobi WHERE heyue_zu.kehu_hb_id = huobi.id) AS kehuduan_bizhong,(SELECT name FROM shichang WHERE heyue_zu.shichang_id = shichang.id) AS shichang,(SELECT name FROM jiaoyi_zu WHERE heyue_zu.jiaoyi_zu_id = jiaoyi_zu.id) AS jiaoyisuo from heyue_zu');
            foreach ($shuju as $key => $value){
                if($value['cancel_shijian']==0 || $value['cancel_shijian']=='' ){
                    $shuju[$key]['cancel_shijian'] = '';
                }else{
                    $shuju[$key]['cancel_shijian'] = date('H:i:s', $value['cancel_shijian']);
                }
                if($value['ping_shijian']==0 || $value['ping_shijian']=='' ){
                    $shuju[$key]['ping_shijian'] = '';
                }else{
                    $shuju[$key]['ping_shijian'] = date('H:i:s', $value['ping_shijian']);
                }
                if($value['ping_shijian1']==0 || $value['ping_shijian1']=='' ){
                    $shuju[$key]['ping_shijian1'] = '';
                }else{
                    $shuju[$key]['ping_shijian1'] = date('H:i:s', $value['ping_shijian1']);
                }
                if($value['geye_shijian']==0 || $value['geye_shijian']=='' ){
                    $shuju[$key]['ping_shijian'] = '';
                }else{
                    $shuju[$key]['geye_shijian'] = date('H:i:s', $value['geye_shijian']);
                }
                if($value['jiekou_id']==1){
                    $shuju[$key]['jiekou_name'] = '内盘';
                }else{
                    $shuju[$key]['jiekou_name'] = '外盘';
                }
            }
            $result['code']=1;
            $result['shuju']= $shuju;
            $result['msg']=urlencode('请求数据成功');
            return urldecode(json_encode($result));
        }else{
            $result['code']=0;
            $result['msg']=urlencode('该用户不存在！');
            return urldecode(json_encode($result));
        }
    }
    public function pinzhong_add_shuju()
    {
        $huobi = db('huobi')
            ->field('id,name')
            ->where('bz',1)
            ->select();
        $shichang = db('shichang')
            ->field('id,name')
            ->select();
        $jiaoyi_zu =  db('jiaoyi_zu')
            ->select();
        $jiekou = array(
            array("内盘",1),
            array("外盘",2));
        $result['code']=1;
        $result['shuju']= array($huobi,$shichang,$jiaoyi_zu,$jiekou);
        $result['msg']=urlencode('666');
        return urldecode(json_encode($result));
    }
    public function pinzhong_add_or_edit()
    {
        $data = input();
        $data = collection($data)->toArray();
        if(empty($data['token'])){
            $result['code']=0;
            $result['msg']=urlencode('非法访问！');
            return urldecode(json_encode($result));
        }
        $name = base64_decode($data['token']);
        $token_check = db('guanli')
            ->where('bh',$name)
            ->find();
        if(count($token_check)>0){
            $detail = $data['shuju'];

                if($detail['cancel_shijian']=='0:00:00'){
                    $detail['cancel_shijian'] = 0;
                }else{
                    $shijian = '1980-11-20 '.$detail['cancel_shijian'];
                    $detail['cancel_shijian'] =  strtotime($shijian);
                }
                if($detail['ping_shijian']=='0:00:00'){
                    $detail['ping_shijian'] = 0;
                }else{
                    $shijian = '1980-11-20 '.$detail['ping_shijian'];
                    $detail['ping_shijian'] =  strtotime($shijian);
                }
                if($detail['ping_shijian1']=='0:00:00'){
                    $detail['ping_shijian1'] = 0;
                }else{
                    $shijian = '1980-11-20 '.$detail['ping_shijian1'];
                    $detail['ping_shijian1'] =  strtotime($shijian);
                }
                if($detail['geye_shijian']=='0:00:00'){
                    $detail['geye_shijian'] = 0;
                }else{
                    $shijian = '1980-11-20 '.$detail['geye_shijian'];
                    $detail['geye_shijian'] =  strtotime($shijian);
                }

            //dump($data_detail);die();
            $pinzhong = model('HeyueZu');
                if(empty($detail['id'])){
                    $pinzhong->data($detail);
                    $res = $pinzhong -> allowField(true) -> save();
                }else{
                    $res = $pinzhong -> allowField(true)->save($detail,['id' => $detail['id']]);
                }
            if($res>0){
                $redis = $this -> redis;
                $re_name = 'update_bz';
                $re_value = '1';
                $redis->set($re_name,$re_value);
                $result['code']=1;
                $result['msg']=urlencode('提交成功');
                return urldecode(json_encode($result));
            }else{
                $result['code']=0;
                $result['msg']=urlencode('数据提交失败，请检查后再次提交！');
                return urldecode(json_encode($result));
            }
        }else{
            $result['code']=0;
            $result['msg']=urlencode('用户不存在！');
            return urldecode(json_encode($result));
        }
    }

}